<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Extra extends CI_Controller{
    
    //Mang tham so nhan duoc o moi action
    protected $_arrParam;
    
    // Mang tham so duong dan upload
    protected $_controllerConfig;
    
    public function __construct(){        
        parent::__construct();
        require_once(APPPATH . 'modules/backend/autoload.php');
		$this->load->language('backend');
		$this->smarty->assign('language', $this->lang);
		$this->smarty->assign('message',$this->session->flashdata('message'));
		$this->smarty->assign('error_message',$this->session->flashdata('error_message'));
        
        //khai bao thu muc upload 	
        $this->_controllerConfig  = array('imagesDir'=> FILES_PATH.'/images/');		
		$this->_arrParam['controllerConfig'] = $this->_controllerConfig;
        
        $jqxtheme = array(base_url() . 'library/jqwidgets/styles/jqx.darkblue.css');
        
        $jqueryui = array(base_url() . 'jscripts/jquery-ui/jquery-ui-1.8.21.min.js');
        
        $css_files = array(base_url() . 'jscripts/jqxGrid/styles/jqx.base.css');

        $js_files = array(
                base_url() . 'jscripts/jqxGrid/jqx-all.js',
                base_url() . 'jscripts/tiny_mce/tiny_mce.js',
                base_url() . 'jscripts/tiny_mce/tinymce_config.js'
        );
        
        //$this->smarty->assign('jqueryui', $jqueryui);
        $this->smarty->assign('jqxtheme', $jqxtheme);
        $this->smarty->assign('css_files', $css_files);
        $this->smarty->assign('js_files', $js_files);
        
        require_once(APPPATH . 'modules/backend/denied.php');
    }
    
    public function banner(){   
        $this->smarty->assign('title','Admin Panel - Quản lý banner : Danh sách banner');
        $this->smarty->assign('heading','QUẢN LÝ BANNER : DANH SÁCH BANNER');
        $jqxgrid_init = $this->smarty->get_contents('backend/js/jqx_banner');
        $js_text = array($jqxgrid_init);
        $this->smarty->assign('js_text', $js_text);
        
        $this->smarty->view('backend/extra/banner', 'grid');
        $this->smarty->display('backend/admin');
        
        $user = $this->login_manager->get_user();
        $aParamAdminLog=array(
                        'username'   => $user[0]->name,
                        'customerID' => $user[0]->customerID,
                        'email'      => $user[0]->email,
                        'lastLogin'  => date("Y-m-d H:i:s"),
                        'logAction'  => "Quản lý banner : Danh sách banner",
                        'ip'         => $_SERVER['REMOTE_ADDR'],
                        'uID'        => $user[0]->uID
                        );
        $log = $this->db_user_model->insertUserAdminLog($aParamAdminLog);
    }
    
    public function data_json_banner(){
        if(IS_AJAX){
            $this->db_advertising_model->actionAdvertising($this->_arrParam, array('task'=>'admin-list'));
        }
    }
    
    public function deleteBanner(){
        $this->db_advertising_model->deleteBanner($this->_arrParam);
    }
    
    public function changeStatusBanner(){
        $this->db_advertising_model->changeStatus($this->_arrParam);
        $user = $this->login_manager->get_user();
        if(isset($this->_arrParam['update'])){
            $userlog = $this->db_user_model->getADV($this->_arrParam['id']);
            foreach($userlog as $value){
                $catName = $value->title;
            }
            $aParamAdminLog=array(
                        'username'   => $user[0]->name,
                        'customerID' => $user[0]->customerID,
                        'email'      => $user[0]->email,
                        'lastLogin'  => date("Y-m-d H:i:s"),
                        'logAction'  => "Quản lý banner : Danh sách banner : ".$catName. " : Status = ".$this->_arrParam['status'],
                        'ip'         => $_SERVER['REMOTE_ADDR'],
                        'uID'        => $user[0]->uID
                        );
            $log = $this->db_user_model->insertUserAdminLog($aParamAdminLog);
        }
    }
    
    function select($str){ // Validation Select
        if($str == 0){
            $this->form_validation->set_message('select', 'Trường %s là bắt buộc.');
			return FALSE;
        }else{
            return TRUE;
        }
    } 
    
    public function bannerInsert(){
        $this->load->helper('form');
        $this->load->library('form_validation');
        
        /** Validator Form  */
        $this->form_validation->set_error_delimiters('<div class="error-form">','</div>'); 
        $this->form_validation->set_rules('title','Tiêu đề ','required');
        //$this->form_validation->set_rules('link','Link ','required');
        $this->form_validation->set_rules('position','Vị trí ','require|callback_select');
        $this->form_validation->set_rules('active','Options ','required');
        if (empty($_FILES['resource']['name'])){
            $this->form_validation->set_rules('resource','Hình ảnh ','required');
        }
                
        if($this->form_validation->run() == FALSE){ 
            $this->smarty->assign('title','Admin Panel - Quản lý banner : Thêm banner');
            $this->smarty->assign('heading','QUẢN LÝ BANNER : THÊM BANNER');
            
            $this->smarty->view('backend/extra/bannerinsert', 'grid');
            $this->smarty->display('backend/admin');

        }else{
            // Success
            $uploadDir = $this->_controllerConfig['imagesDir'];
            $config['upload_path'] = $uploadDir.'banner';
            $config['allowed_types'] = 'gif|jpg|png|jpeg';
            $file_name = time().$_FILES['resource']['name'];
            $config['file_name'] = $file_name;
            $this->load->library('upload', $config);
            
            if($this->upload->do_upload('resource')){
                /** Upload image */
                $file_success = $this->upload->data();
                
                $config = array("source_image" => $file_success['full_path'],
                                "new_image" => $uploadDir."banner/thumbs",
                                "maintain_ration" => true,
                                "width" => '200',
                                "height" => "150");
                $this->load->library("image_lib",$config);
                $this->image_lib->resize();
                
                $user = $this->login_manager->get_user();
                
                /** insert params */
                $this->db_advertising_model->actionAdvertising($this->_arrParam, array('task'=>'admin-add'), $user[0], $file_success['file_name']);
                
                /** insert adminlog */
                $aParamAdminLog=array(
                                'username'   => $user[0]->name,
                                'customerID' => $user[0]->customerID,
                                'email'      => $user[0]->email,
                                'lastLogin'  => date("Y-m-d H:i:s"),
                                'logAction'  => "Quản lý banner : Thêm banner : ".$this->_arrParam['title'],
                                'ip'         => $_SERVER['REMOTE_ADDR'],
                                'uID'        => $user[0]->uID
                                );
                $log = $this->db_user_model->insertUserAdminLog($aParamAdminLog);
                
                //redirect(base_url('admin/news/newsList', 'refresh'));
                echo "<script>location.href='".base_url('admin/extra/banner')."'</script>";         
            }           
        }
    }
    
    public function bannerUpdate(){
        $this->load->helper('form');
                
        $this->smarty->assign('title','Admin Panel - Quản lý banner : Sửa banner');
        $this->smarty->assign('heading','QUẢN LÝ BANNER : SỬA BANNER');
        
        $bannerRow= $this->db_advertising_model->getWhererow($this->_arrParam, array('task' => 'where-edit'));
        $this->smarty->assign("bannerRow", $bannerRow);
        
        $this->smarty->view('backend/extra/bannerupdate', 'grid');
        $this->smarty->display('backend/admin');
        
        if(isset($this->_arrParam['submit'])){
            
            $uploadDir = $this->_controllerConfig['imagesDir'];
            $config['upload_path'] = $uploadDir.'banner';
            $config['allowed_types'] = 'gif|jpg|png|jpeg';
            $file_name = time().$_FILES['resource']['name'];
            $config['file_name'] = $file_name;
            $this->load->library('upload', $config);
            
            $images = null;
            
            if($this->upload->do_upload('resource')){

                $file_success = $this->upload->data();
                
                $config = array("source_image" => $file_success['full_path'],
                                "new_image" => $uploadDir."banner/thumbs",
                                "maintain_ration" => true,
                                "width" => '200',
                                "height" => "150");
                $this->load->library("image_lib",$config);
                $this->image_lib->resize();
                
                $images = $file_success['file_name'];
                
                /** Remove hình cũ */
                $Result = $this->db_advertising_model->getWhererow($this->_arrParam, array('task' => 'where-removeImg'));
                foreach($Result as $image){
                    @unlink($uploadDir . 'banner/' . $image->resource);
                    @unlink($uploadDir . 'banner/thumbs/' . $image->resource);						
                }
                
            }else{
                $images = $this->_arrParam['image_temp'];
            }    
            $user = $this->login_manager->get_user();
            
            /** Update params */
            $this->db_advertising_model->actionAdvertising($this->_arrParam, array('task'=>'admin-edit'), $user[0], $images);
            
            /** insert adminlog */
            $aParamAdminLog=array(
                            'username'   => $user[0]->name,
                            'customerID' => $user[0]->customerID,
                            'email'      => $user[0]->email,
                            'lastLogin'  => date("Y-m-d H:i:s"),
                            'logAction'  => "Quản lý banner : Sửa banner : ".$this->_arrParam['title'],
                            'ip'         => $_SERVER['REMOTE_ADDR'],
                            'uID'        => $user[0]->uID
                            );
            $log = $this->db_user_model->insertUserAdminLog($aParamAdminLog);

            echo "<script>location.href='".base_url('admin/extra/banner')."'</script>";         
        }
    }
}

/* End of file extra.php */
/* Location: ./system/application/controllers/backend/extra.php */